package com.eastfair.home.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.annotation.annotation.log.SysLog;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.base.R;
import com.eastfair.core.exception.BizException;
import com.eastfair.echo.core.EchoService;
import com.eastfair.home.dto.*;
import com.eastfair.home.entity.BoothAcceptanceRecord;
import com.eastfair.home.enumeration.BoothAcceptanceRecordAcceptanceStatusEnum;
import com.eastfair.home.enumeration.BoothAcceptanceRecordAcceptanceTypeEnum;
import com.eastfair.home.service.BoothAcceptanceRecordService;
import com.eastfair.home.vo.BoothAcceptanceDetailVO;
import com.eastfair.home.vo.BoothAcceptanceRecordVO;
import com.eastfair.home.vo.SupervisionItemQuestionVO;
import com.eastfair.venuebooking.vo.SpaceVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 展台验收记录
 * </p>
 *
 * @author linan
 * @date 2022-11-29
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/boothAcceptanceRecord")
@Api(value = "BoothAcceptanceRecord", tags = "展台验收记录")
@PreAuth(replace = "home:boothAcceptanceRecord:")
public class BoothAcceptanceRecordController extends SuperController<BoothAcceptanceRecordService, Long, BoothAcceptanceRecord, BoothAcceptanceRecordPageQuery, BoothAcceptanceRecordSaveDTO, BoothAcceptanceRecordUpdateDTO> {

    @Autowired
    private EchoService echoService;

    @Override
    public void handlerResult(IPage<BoothAcceptanceRecord> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }
    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<BoothAcceptanceRecord> boothAcceptanceRecordList = list.stream().map((map) -> {
            BoothAcceptanceRecord boothAcceptanceRecord = BoothAcceptanceRecord.builder().build();
            //TODO 请在这里完成转换
            return boothAcceptanceRecord;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(boothAcceptanceRecordList));
    }



    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<BoothAcceptanceRecordPageQuery> params) {
        BoothAcceptanceRecordPageQuery pageQuery = params.getModel();
    }

    /**
     * 查询展台验收记录
     * @param query query
     * @return R
     */
    @PostMapping(value = "/getBoothAcceptanceRecord")
    public R<List<BoothAcceptanceRecordVO>> getBoothAcceptanceRecord(@RequestBody BoothAcceptanceRecordQuery query){
       return R.success(baseService.getBoothAcceptanceRecord(query));
    }

    /**
     * 查询展台验收明细
     * @param query query
     * @return R
     */
    @PostMapping(value = "/getBoothAcceptanceDetail")
    public R<BoothAcceptanceRecordVO> getBoothAcceptanceDetail(@RequestBody BoothAcceptanceRecordQuery query){
       return R.success(baseService.getBoothAcceptanceDetail(query));
    }

    /**
     * 保存展台验收明细
     * @param dto dto
     * @return R
     */
    @SysLog("添加展台验收明细")
    @PostMapping(value = "/saveBoothAcceptanceDetail")
    public R<Boolean> saveBoothAcceptanceDetail(@RequestBody BoothAcceptanceRecordDTO dto){
        try {
            return R.success(baseService.saveBoothAcceptanceDetail(dto));
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * 发起展台验收.
     *
     * @return the r
     */
    @ApiOperation("发起展台验收")
    @SysLog("发起展台验收")
    @PostMapping("/arrangementAcceptance")
    public R<BoothAcceptanceRecord> arrangementAcceptance(@RequestBody BoothAcceptanceRecordDTO boothAcceptanceRecordDTO){
        boothAcceptanceRecordDTO.setAcceptanceType(BoothAcceptanceRecordAcceptanceTypeEnum.ARRANGEMENT);
        boothAcceptanceRecordDTO.setAcceptanceStatus(BoothAcceptanceRecordAcceptanceStatusEnum.REQUESTED);
        return R.success(baseService.saveAcceptance(boothAcceptanceRecordDTO));

    }

    /**
     * 发起撤展验收.
     * @return the r
     */
    @ApiOperation("发起撤展验收")
    @SysLog("发起撤展验收")
    @PostMapping("/dismantlingAcceptance")
    public R<BoothAcceptanceRecord> dismantlingAcceptance(@RequestBody BoothAcceptanceRecordDTO boothAcceptanceRecordDTO){
        boothAcceptanceRecordDTO.setAcceptanceType(BoothAcceptanceRecordAcceptanceTypeEnum.DISMANTLING);
        boothAcceptanceRecordDTO.setAcceptanceStatus(BoothAcceptanceRecordAcceptanceStatusEnum.REQUESTED);
        return R.success(baseService.saveAcceptance(boothAcceptanceRecordDTO));
    }

    /**
     * 获取验收通过的展厅.
     * @return the r
     */
    @ApiOperation("获取验收通过的展厅")
    @PostMapping("/getSpaceOfPassSite")
    public R<List<SpaceVO>> getSpaceOfPassSite(@RequestBody BoothAcceptanceRecordDTO boothAcceptanceRecordDTO){
        return R.success(baseService.getPassSpace(boothAcceptanceRecordDTO));
    }

}
